define([], function() {
    return {
        init: function(common) {
            commonObj = {
                toGetData:function(url='', data={},type='GET',dataType='json') {
                    var dataArr = [];
                    var htmlobj = $.ajax({
                    url: url,
                    async: false,
                    method: type,
                    data: data,
                    'dataType': dataType
                    });
                    var jsonObj = htmlobj.responseText;
                    var dataArr = $.parseJSON(jsonObj);
                    return dataArr;
                },
                reload: function(obj) {
                    var currTab = $('#tabs').tabs('getSelected'); //获得当前tab
                    var url = currTab.panel('options').href;
                    $('#tabs').tabs('update', {
                        tab: currTab,
                        options: {
                            href: url
                        }
                    });
                    currTab.panel('refresh');
                },
                update: function(url) {
                    var currTab = $('#tabs').tabs('getSelected');
                    $("#tabs").tabs('update', {
                        tab: currTab,
                        options: {
                            href: url
                        }
                    });
                    currTab.panel('refresh');
                },
                openTab: function(obj) {
                    if (obj.url) {
                        if ($("#tabs").tabs('exists', $.trim(obj.text))) {
                            $("#tabs").tabs('select', $.trim(obj.text));
                            //是否重新加载
                            if (obj.reload) {
                                commonObj.reload();
                            }
                            //是否更新内容
                            if(obj.update){
                                commonObj.update(obj.url);
                            }
                        } else {
                            $("#tabs").tabs('add', {
                                title: $.trim(obj.text),
                                closable: true,
                                href: obj.url,
                                iconCls: obj.iconCls,
                            });
                        }
                    }
                },
                tabsClose: function(obj) {
                    var tab = $('#tabs').tabs('getSelected'); //获取当前选中tabs  
                    var index = $('#tabs').tabs('getTabIndex', tab); //获取当前选中tabs的index  
                    $('#tabs').tabs('close', index); //关闭对应index的tabs  
                },
                upload: function(obj) {
                    var uploader = new plupload.Uploader({
                        runtimes: 'html5,flash,silverlight,html4',
                        browse_button: obj.btn,
                        url: obj.url,
                        filters: {
                            max_file_size: '2000kb',
                            mime_types: [{
                                title: "thumbs",
                                extensions: "jpg,png,gif"
                            }]
                        },
                        multipart_params: {
                            path: obj.path
                        },
                        multi_selection: false,
                        init: {
                            FilesAdded: function(up, files) {
                                if (files.length > 2) {
                                    alert("您上传的图片太多了！");
                                    uploader.destroy();
                                } else {
                                    uploader.start();
                                }
                            },
                            UploadProgress: function(up, file) {},
                            FileUploaded: function(up, file, info) {
                                var retJson = $.parseJSON(info.response);
                                if (parseInt(retJson.status)) {
                                    $.messager.alert('error', retJson.msg, 'error');
                                    return false;
                                }
                                var img_url = retJson.data.thumbs;
                                $(obj.imgObj).attr('src', img_url);
                                $(obj.inputObj).val(img_url);
                            },
                            Error: function(up, err) {
                                $.messager.alert('error', err, 'error');
                            }
                        }
                    });
                    uploader.init();
                }
            };
        }
    }
});